<template>
  <div class="hs-panel hs-bg-card">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px" class="filter-item"
                v-model="listQuery.name"></el-input>
      <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="groupTypeManager_btn_add"
                 style="float:right" @click="handleAdd"
                 type="primary" icon="el-icon-plus" circle>
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading"
              border fit highlight-current-row
              style="width: 100%; margin:10px 0">

      <el-table-column type="index" width="50" label="序号"></el-table-column>
      <el-table-column width="120" label="编码" prop="code">
      </el-table-column>
      <el-table-column width="120" align="center" label="类型名称" prop="name">
      </el-table-column>
      <el-table-column width="120" align="center" label="描述" prop="description">
      </el-table-column>
      <el-table-column width="160" align="center" label="最后更新" prop="updatedAt">
      </el-table-column>
      <el-table-column width="80" align="center" label="更新人" prop="updatedBy">
      </el-table-column>
      <el-table-column width="80" align="center" label="更新IP" prop="updatedFrom">
      </el-table-column>
      <el-table-column align="center" label="操作"  fixed="right" width="100">
        <template slot-scope="scope">
          <el-button v-if="groupTypeManager_btn_edit" size="small"
                     type="primary" icon="el-icon-edit" circle
                     @click="handleUpdate(scope.row)">
          </el-button>
          <el-button v-if="groupTypeManager_btn_del" size="small"
                     type="danger" icon="el-icon-delete" circle
                     @click="handleDelete(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.pageNum"
                     :page-sizes="[20, 30, 50]"
                     :page-size="listQuery.pageSize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[editDialog.type]" :visible.sync="editDialog.show">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码"></el-input>
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入类型名称"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editDialog.show = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { page, addObj, delObj, putObj } from 'api/admin/roleType'
import { mapGetters } from 'vuex'
export default {
  name: 'groupType',
  data () {
    return {
      form: {
        code: undefined,
        name: undefined,
        description: undefined
      },
      rules: {
        code: [
          { required: true, message: '请输入编码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入编码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        name: undefined
      },
      editDialog: {
        type: 'update',
        show: false
      },
      groupTypeManager_btn_edit: false,
      groupTypeManager_btn_del: false,
      groupTypeManager_btn_add: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created () {
    this.getList()
    if (this.elements) {
      this.groupTypeManager_btn_edit = this.elements['roleType:update']
      this.groupTypeManager_btn_del = this.elements['roleType:delete']
      this.groupTypeManager_btn_add = this.elements['roleType:add']
    }
  },
  computed: {
    ...mapGetters({
      elements: 'userElements'
    })
  },
  methods: {
    getList () {
      this.listLoading = true
      page(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter () {
      this.getList()
    },
    handleSizeChange (val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleAdd () {
      this.form.id = 0
      this.editDialog.type = 'create'
      this.editDialog.show = true
    },
    handleUpdate (row) {
      this.form.id = row.id
      this.form.code = row.code
      this.form.name = row.name
      this.form.description = row.description
      this.editDialog.type = 'update'
      this.editDialog.show = true
    },
    handleDelete (row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delObj(row.id).then(() => {
          this.$message.success('删除成功')
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      })
    },
    submit () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id > 0) {
            this.doUpdate()
          } else {
            this.doAdd()
          }
        }
      })
    },
    doAdd () {
      addObj(this.form).then(() => {
        this.complete()
      })
    },
    doUpdate () {
      this.form.password = undefined
      putObj(this.form.id, this.form).then(() => {
        this.complete()
      })
    },
    complete () {
      this.editDialog.show = false
      this.getList()
      this.$refs['form'].resetFields()
      this.$message.success('操作成功')
    }
  }
}
</script>
